package com.taiyi.renaissancemall.ui.login

import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.widget.TextView
import com.taiyi.renaissancemall.R
import com.taiyi.renaissancemall.base.BaseActivity
import com.taiyi.renaissancemall.ui.MainActivity
import kotlinx.android.synthetic.main.activity_bind_wechat.*

class BindWechatActivity : BaseActivity<BindWechatView, BindWechatPresent>(), BindWechatView {


    override fun getLayoutId(): Int = R.layout.activity_bind_wechat

    override fun initPresenter(): BindWechatPresent = BindWechatPresent()

    override fun initViews(savedInstanceState: Bundle?) {

    }

    override fun initData() {

    }

    override fun initEvent() {
        getVerificationCodeTv.setOnClickListener {
            showProgress()
            presenter.getVerificationCode()
        }

        confirmTv.setOnClickListener {
            showProgress()
            presenter.bindPhoneNumber()
        }
    }

    override fun onBindSuccess() {
        hideProgress()
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }

    override fun getVerificationCodeSuccess() {
        hideProgress()
        var mTimeCount = TimeCount(60000, 1000, getVerificationCodeTv)
        mTimeCount.start()
    }

    class TimeCount
    /**
     * @param millisInFuture    The number of millis in the future from the call
     * to [.start] until the countdown is done and [.onFinish]
     * is called.
     * @param countDownInterval The interval along the way to receive
     * [.onTick] callbacks.
     */(millisInFuture: Long, countDownInterval: Long, var tv: TextView) :
        CountDownTimer(millisInFuture, countDownInterval) {
        override fun onTick(millisUntilFinished: Long) {
            tv.isEnabled = false
            val time =
                if (millisUntilFinished % 1000 > 500) millisUntilFinished / 1000 + 1 else millisUntilFinished / 1000
            tv.text = (time.toString() + "秒")
        }

        override fun onFinish() {
            tv.text = "重新获取"
            tv.isEnabled = true

        }
    }


    override fun getVerificationCode(): String = verificationCodeEt.text.toString()

    override fun getPhoneNumber(): String = phoneNumberEt.text.toString()


    override fun getVerificationCodeFailure(msg: String?) {
        hideProgress()
        toast(msg)
    }

    override fun onBindFailure(msg: String?) {
        hideProgress()
        toast(msg)
    }

    override fun getInviteCode(): String = inviteCodeEt.text.toString()
}
